package com.cman777.springc.encode.advice.d4t;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenzhicong
 * @time 2020/5/26 12:28
 */
@Slf4j
@Configuration
public class EncodingConfig {

    /**
     * 清除本地变量的filter
     *  清除原因：每个请求线程可以被重用，为了避免冲突
     */
    @Bean
    public FilterRegistrationBean clearThreadLocalFilter() {
        FilterRegistrationBean frBean = new FilterRegistrationBean();
        frBean.setFilter((servletRequest, servletResponse, filterChain) -> {
            try{
                filterChain.doFilter(servletRequest, servletResponse);
            }finally{
                AesKeyHolder.clear();
            }

        });
        frBean.setOrder(Integer.MIN_VALUE);
        frBean.setName("clearThreadLocalFilter");
        frBean.addUrlPatterns("/*");
        return frBean;
    }

}
